/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.asset.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import top.continew.admin.asset.model.entity.DeviceDiscardDO;
import top.continew.admin.asset.model.entity.DeviceLendDO;
import top.continew.admin.asset.model.entity.DeviceUseDO;
import top.continew.admin.asset.model.resp.DeviceTracksResp;
import top.continew.admin.asset.service.DeviceTracksService;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.admin.common.controller.BaseController;
import top.continew.admin.asset.model.query.DeviceRecordsQuery;
import top.continew.admin.asset.model.req.DeviceRecordsReq;
import top.continew.admin.asset.model.resp.DeviceRecordsDetailResp;
import top.continew.admin.asset.model.resp.DeviceRecordsResp;
import top.continew.admin.asset.service.DeviceRecordsService;
import top.continew.starter.extension.crud.model.resp.IdResp;

import java.util.List;

/**
 * 资产管理-设备-清单 API
 *
 * @author CharlieYan
 * @since 2025/03/18 10:12
 */
@Tag(name = "资产管理-设备-清单 API")
@RestController
@Validated
@RequiredArgsConstructor
@CrudRequestMapping(value = "/asset/device/records", api = {Api.PAGE, Api.GET, Api.CREATE, Api.UPDATE, Api.DELETE,
    Api.EXPORT})
public class DeviceRecordsController extends BaseController<DeviceRecordsService, DeviceRecordsResp, DeviceRecordsDetailResp, DeviceRecordsQuery, DeviceRecordsReq> {

    private final DeviceRecordsService deviceRecordsService;

    private final DeviceTracksService deviceTracksService;

    @Operation(summary = "设备-撤销删除", description = "设备-撤销删除")
    @SaCheckPermission("asset:device:records:delete:return")
    @GetMapping(value = "/delete/return/{id}")
    public IdResp<Long> deleteReturn(@PathVariable Long id) {
        return new IdResp(deviceRecordsService.deleteReturn(id));
    }

    @Operation(summary = "设备-履历", description = "设备-履历")
    @SaCheckPermission("asset:device:records:get")
    @GetMapping(value = "/tracks/{id}")
    public List<DeviceTracksResp> deviceTracks(@PathVariable Long id) {
        return deviceTracksService.deviceTracks(id);
    }

    @Operation(summary = "设备-分配", description = "设备-分配")
    @SaCheckPermission("asset:device:records:use")
    @PostMapping(value = "/use")
    public IdResp<Long> deviceUse(@RequestBody DeviceUseDO useDO) {
        return new IdResp(deviceRecordsService.deviceUse(useDO));
    }

    @Operation(summary = "设备-借用", description = "设备-借用")
    @SaCheckPermission("asset:device:records:lend")
    @PostMapping(value = "/lend")
    public IdResp<Long> deviceLend(@RequestBody DeviceLendDO lendDO) {
        return new IdResp(deviceRecordsService.deviceLend(lendDO));
    }

    @Operation(summary = "设备-归还", description = "设备-归还")
    @SaCheckPermission("asset:device:records:return")
    @PostMapping(value = "/return")
    public IdResp<Long> deviceReturn(@RequestBody DeviceLendDO lendDO) {
        return new IdResp(deviceRecordsService.deviceReturn(lendDO));
    }

    @Operation(summary = "设备-报废", description = "设备-报废")
    @SaCheckPermission("asset:device:records:discard")
    @PostMapping(value = "/discard")
    public IdResp<Long> deviceDiscard(@RequestBody DeviceDiscardDO discardDO) {
        return new IdResp(deviceRecordsService.deviceDiscard(discardDO));
    }

    @Operation(summary = "设备-撤销报废", description = "设备-撤销报废")
    @SaCheckPermission("asset:device:records:undiscard")
    @PostMapping(value = "/discard/cancel")
    public IdResp<Long> deviceDiscardCancel(@RequestBody DeviceDiscardDO discardDO) {
        return new IdResp(deviceRecordsService.deviceDiscardCancel(discardDO));
    }

}